import { Custom, CustomEnum } from 'custom/index'
import { FormItemRule } from 'element-plus'
import { Arrayable } from 'element-plus/es/utils/typescript.mjs';
export default class FormPageItem extends Custom {
  public static name: CustomEnum = 'FormPageItem';

  public static props = {
    props: {
      type: String,
      default: ''
    },
    label: {
      type: String,
      default: ''
    },
    rules: {
      type: null
    },
    data: {
      type: Object,
      default: () => ({})
    }
  };

  label(label: string) {
    this._props.label = label
    return this
  }

  span(span: number) {
    this._props.span = span
    return this
  }

  rules(rules: Arrayable<FormItemRule>) {
    if (this._props.rules) {
      if (Array.isArray(this._props.rules)) {
        if (Array.isArray(rules)) {
          this._props.rules.push(...rules)
        } else {
          this._props.rules.push(rules)
        }
      } else {
        if (Array.isArray(rules)) {
          this._props.rules = [this._props.rules, ...rules]
        } else {
          this._props.rules = [this._props.rules, rules]
        }
      }
    } else {
      this._props.rules = rules
    }
    return this
  }
  
  required(msg: string = '', trigger: 'blur' | 'change' = 'change') {
    const message = msg || this._props.label ? `${this._props.label}是必填的` : ''
    return this.rules([{ required: true, message: message, trigger }])
  }
}
